//
//  UserDAO.swift
//  DiDiRecycle
//
//  Created by abb on 16/7/28.
//  Copyright © 2016年 abb. All rights reserved.
//

import UIKit

class UserDAO: NSObject {
    class func getUserBySms(phoneNumber:String,zone:String,code:String,finished:(dict:[String:AnyObject]?,resultCode:Int?,error:String?)->()) {
        
        let path = kUrl + "user/smsSDK/"
        
        let params:[String:AnyObject] = [
            "phone_number" : phoneNumber,
            "zone" : zone,
            "code" : code,
            "is_merchant" : 1
        ]
        NetworkTools.shareNetwortTools().request(.POST, path, parameters: params, encoding: .URL, headers: nil).responseJSON { (response) in
            // 判断网络错误
            if response.result.error == nil {
                // 判断数据错误
                if response.result.value!["code"] as! String == "1" {
                    // 获取数据
                    let result = response.result.value!["result"] as! [String:AnyObject]
                    let resultCode = response.result.value!["resultCode"] as! Int
                    // 回调闭包
                    finished(dict: result,resultCode: resultCode, error: nil)
                }
                else {
                    // 获取错误信息
                    let result = response.result.value!["result"] as! String
                    // 回调闭包
                    finished(dict: nil,resultCode: nil,error: result)
                }
            } else {
                // 打印网络错误
                print(response.result.error)
                finished(dict:nil,resultCode: nil, error: "网络错误")
            }
        }
        
    }
    
    class func getUserByPassword(phone_number:String,password:String,finished:(bool:Bool,result:[String:AnyObject]?,error:String?)->()) {
        
        let path = kUrl + "user/getUserByPassword"
        
        let params:[String:AnyObject] = [
            "phone_number" : phone_number,
            "password" : password,
            "is_merchant" : 1
            ]
        NetworkTools.shareNetwortTools().request(.POST, path, parameters: params, encoding: .URL, headers: nil).responseJSON { (response) in
            if response.result.error == nil {
                let bool = response.result.value!["code"] as! String == "1" ? true : false
                print(response.result.value!)
                if bool {
                    let result = response.result.value!["result"] as! [String:AnyObject]
                    finished(bool: bool, result: result,error: nil)
                } else {
                    let result = response.result.value!["result"] as! String
                    finished(bool: bool,result: nil,error: result)
                }
                
            } else {
                print(response.result.error)
                finished(bool: false, result: nil,error: "网络错误")
            }
            
        }
    }
    
    class func setUserInfo(phone_number:String,nickName:String,password:String,finished:(bool:Bool,result:String)->()) {
        let path = kUrl + "user/setUserInfo"
        
        let params = [
            "phone_number" : phone_number,
            "nick_name" : nickName,
            "password" : password,
            ]
        NetworkTools.shareNetwortTools().request(.POST, path, parameters: params, encoding: .URL, headers: nil).responseJSON { (response) in
            if response.result.error == nil {
                let bool = response.result.value!["code"] as! String == "1" ? true : false
                let result = response.result.value!["result"] as! String
                finished(bool: bool, result: result)
            } else {
                print(response.result.error)
                finished(bool: false, result: "网络错误")
            }
            
        }
    }
    
    class func setIcon(icons:UIImage,finished:(dict:[String:AnyObject]?,error:String?)->()) {
        
        let path = kUrl + "user/uploadIcon"
        
        NetworkTools.shareNetwortTools().upload(.POST, path, multipartFormData: { (MultipartFormData) in
            let data = UIImagePNGRepresentation(icons)
            MultipartFormData.appendBodyPart(data: data!, name: "image", fileName: "abb.png", mimeType: "image/png")
            let dataStr = String(User.loadUser()!.id).dataUsingEncoding(NSUTF8StringEncoding)
            MultipartFormData.appendBodyPart(data: dataStr!, name: "user_id")
            }) { (EncodingResult) in
                switch EncodingResult {
                case .Success(request: let request, streamingFromDisk: _, streamFileURL: _):

                    request.responseJSON(completionHandler: { (response) in
                        if response.result.error == nil {
                            if response.result.value!["code"] as! String == "1" {
                                let result = response.result.value!["result"]!!["userimage"] as! [String:AnyObject]
                                finished(dict: result, error: nil)
                            } else {
                                let result = response.result.value!["result"] as! String
                                finished(dict: nil, error: result)
                            }
                        } else {
                            print(response.result.error)
                            finished(dict: nil, error: "网络错误")
                        }
                    })
                case .Failure(let _):
                    finished(dict: nil, error: "上传失败")
                }
        }
    }
    
    
    class func updateNickName(newText:String,finished:(bool:Bool,result:String)->()) {
        
        let path = kUrl + "user/updateNickName"
        
        let params = [
            "nick_name" : newText,
            "phone_number" : User.loadUser()!.phoneNumber!
        ]
        
        NetworkTools.shareNetwortTools().request(.POST, path, parameters: params, encoding: .URL, headers: nil).responseJSON { (response) in
            if response.result.error == nil {
                let bool = response.result.value!["code"] as! String == "1" ? true : false
                let result = response.result.value!["result"] as! String
                finished(bool: bool, result: result)
            } else {
                print(response.result.error)
                finished(bool: false, result: "网络错误")
            }
        }
    }
    
    class func getUserByMerchant(userID:Int,finished:(bool:Bool,result:[String:AnyObject]?,error:String?)->()) {
        
        let path = kUrl + "user/getUserByMerchant"
        
        let params = [
            "id" : userID
        ]
        
        NetworkTools.shareNetwortTools().request(.POST, path, parameters: params, encoding: .URL, headers: nil).responseJSON { (response) in
            if response.result.error == nil {
                if response.result.value!["code"] as! String == "1" {
                    let result = response.result.value!["result"] as! [String:AnyObject]
                    finished(bool: true, result: result,error: nil)
                    return
                }
                finished(bool: false, result: nil,error: "未找到用户")
                
            } else {
                finished(bool: false, result: nil,error: "网络错误")
            }
        }
    }
}
